package com.nexgo.payment.trans.download;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.RecvData;
import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.aop.annotation.RunOnWorkThread;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.Blacklist;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.trans.AbsOnlinePresenter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by xiaox on 2017/8/12.
 */
public class BlacklistPresenter extends AbsOnlinePresenter implements DownloadTransContract.Presenter {
    private Logger mLog = LoggerFactory.getLogger(BlacklistPresenter.class.getSimpleName());

    private DownloadTransContract.View mView;

    protected BlacklistPresenter(@NonNull DownloadTransContract.View view, @NonNull DataSource repository) {
        super(view, repository);
        mView = checkNotNull(view);
    }

    @Override
    protected TransData getTransData() {
        TransData transData = new TransData();
        transData.setTransType(TransType.BLACKLIST_DOWN);
        return transData;
    }

    @Override
    protected void onFirstAction() {
        postMainThread(() -> mView.showOnlineProc(mTransData, App.sContext.getString(R.string.black_down)));
    }

    @Override
    @RunOnWorkThread
    protected void onTransSuccess(RecvData recvData) {
        if (TransType.BLACKLIST_DOWN.equals(mTransData.getTransType())) {
            if (TextUtils.isEmpty(recvData.getField62())) {
                postMainThread(() -> mView.showTransFailed());
                return;
            }
            int offset = 0;
            String data = new String(ByteUtils.hexString2ByteArray(recvData.getField62()));
            if (data.startsWith("0") || data.length() <= 1) {
                //无黑名单，或是错误
                mTransData.setTransType(TransType.BLACKLIST_END);
                postMainThread(() -> mView.showOnlineProc(mTransData, App.sContext.getString(R.string.black_end)));
                return;
            }
            offset++;
            Integer lastItem = Integer.parseInt(data.substring(offset, offset + 3));
            offset += 3;
            if (mTransData.getCounter() == 0) {
                //第一次下载删除所有的黑名单
                mRepository.clearBlacklist();
            }
            postMainThread(() -> mView.setWaitingIndicator(true, null, App.sContext.getString(R.string.doing_something)));
            for (int i = 0; i < lastItem + 1 - mTransData.getCounter(); i++) {
                int len = Integer.parseInt(data.substring(offset, offset + 2));
                offset += 2;
                String bin = data.substring(offset, offset + len);
                Blacklist blacklist = new Blacklist();
                blacklist.setCardBin(bin);
                mRepository.insertBlacklist(blacklist);
            }
            postMainThread(() -> mView.setWaitingIndicator(false, null, null));
            if (data.startsWith("1") || lastItem < mTransData.getCounter()) {
                //无后续
                mTransData.setTransType(TransType.BLACKLIST_END);
                postMainThread(() -> mView.showOnlineProc(mTransData, App.sContext.getString(R.string.black_end)));
            } else {
                mTransData.setCounter(lastItem + 1);
                postMainThread(() -> mView.showOnlineProc(mTransData, App.sContext.getString(R.string.black_down)));
            }
        } else {
            postMainThread(() -> mView.showTransSuccess());
        }
    }
}
